<template>
  <a-modal
    v-model:open="visible"
    :width="500"
    :confirm-loading="loading"
    ok-text="删除"
    cancel-text="取消"
    ok-type="danger"
    @ok="handleConfirm"
    @cancel="handleCancel"
  >
    <div class="delete-content">
      <div class="warning-icon">
        <AlertTriangle />
      </div>
      
      <div class="delete-info">
        <h3 class="delete-title">确认删除默认角色？</h3>
        <p class="delete-description">
          您即将删除默认角色 <strong>"{{ role?.roleName }}"</strong>，此操作不可撤销。
        </p>
        
        <div v-if="role" class="role-preview">
          <div class="role-avatar">
            <a-avatar 
              :size="48" 
              :src="role.avatarUrl || role.avatar"
              :style="{ 
                background: role.avatarUrl || role.avatar ? 'transparent' : 'linear-gradient(135deg, #6366f1, #8b5cf6)',
                fontSize: '20px',
                fontWeight: '600'
              }"
            >
              {{ !role.avatarUrl && !role.avatar ? (role.roleName?.[0] || 'R') : '' }}
            </a-avatar>
          </div>
          <div class="role-details">
            <div class="role-name">{{ role.roleName }}</div>
            <div class="role-tags">
              <!-- 默认角色标识 -->
              <a-tag color="gold" size="small">
                <template #icon>
                  <StarFilled />
                </template>
                默认角色
              </a-tag>
              
              <!-- 音色标签 -->
              <a-tag
                v-if="role.voiceId"
                size="small"
                color="purple"
                class="role-tag voice-tag"
              >
                <template #icon>
                  <span class="voice-icon">🎵</span>
                </template>
                {{ getVoiceName(role.voiceId) }}
              </a-tag>
              
              <!-- 知识库标签 -->
              <a-tag
                v-if="role.knowledgeId"
                size="small"
                color="blue"
                class="role-tag knowledge-tag"
              >
                <template #icon>
                  <span class="knowledge-icon">📚</span>
                </template>
                {{ getKnowledgeName(role.knowledgeId) }}
              </a-tag>
            </div>
          </div>
        </div>
        
        <div class="confirmation-input">
          <p class="input-label">
            请输入角色名称 <strong>"{{ role?.roleName }}"</strong> 以确认删除：
          </p>
          <a-input
            v-model:value="confirmText"
            placeholder="请输入角色名称"
            size="large"
            :status="confirmError ? 'error' : ''"
          />
          <div v-if="confirmError" class="error-message">
            角色名称不匹配，请重新输入
          </div>
        </div>

        <div class="warning-notice">
          <a-alert
            message="删除警告"
            description="删除默认角色可能会影响系统的正常运行，请谨慎操作。"
            type="warning"
            show-icon
          />
        </div>
      </div>
    </div>
  </a-modal>
</template>

<script setup lang="ts">
import { ref, computed, watch, onMounted } from 'vue'
import { message } from 'ant-design-vue'
import { AlertTriangle } from 'lucide-vue-next'
import { StarFilled } from '@ant-design/icons-vue'
import roleService, { type ExtendedRole } from '@/api/roleService'
import { useResourceStore } from '@/stores'

// Props 和 Emits
interface Props {
  open: boolean
  role: ExtendedRole | null
}

interface Emits {
  'update:open': [value: boolean]
  'success': [roleId: string]
}

const props = withDefaults(defineProps<Props>(), {
  open: false,
  role: null
})

const emit = defineEmits<Emits>()

// 响应式数据
const visible = ref(false)
const loading = ref(false)
const confirmText = ref('')
const confirmError = ref(false)

// 获取stores
const resourceStore = useResourceStore()

// 计算属性
const isConfirmValid = computed(() => {
  return confirmText.value === props.role?.roleName
})

// 监听 props 变化
watch(() => props.open, (newVal) => {
  visible.value = newVal
  if (newVal) {
    confirmText.value = ''
    confirmError.value = false
  }
})

watch(visible, (newVal) => {
  emit('update:open', newVal)
})

watch(confirmText, () => {
  if (confirmError.value) {
    confirmError.value = false
  }
})

// 方法
const getVoiceName = (voiceId: string) => {
  return resourceStore.getVoiceName(voiceId)
}

const getKnowledgeName = (knowledgeId: string) => {
  return resourceStore.getKnowledgeBaseName(knowledgeId)
}

const handleConfirm = async () => {
  if (!props.role) {
    message.error('角色信息不存在')
    return
  }

  if (!isConfirmValid.value) {
    confirmError.value = true
    return
  }

  try {
    loading.value = true
    
    // 调用API删除角色 - 目前使用普通角色接口，后续需要改为默认角色接口
    const success = await roleService.deleteRole(props.role.id)
    
    if (success) {
      emit('success', props.role.id)
      visible.value = false
      message.success('默认角色删除成功！')
    }
  } catch (error) {
    console.error('删除默认角色失败:', error)
    message.error('删除默认角色失败，请重试')
  } finally {
    loading.value = false
  }
}

const handleCancel = () => {
  visible.value = false
}

// 组件挂载时初始化资源数据
onMounted(() => {
  resourceStore.initResource()
})
</script>

<style scoped>
.delete-content {
  display: flex;
  gap: 16px;
  padding: 16px 0;
}

.warning-icon {
  flex-shrink: 0;
  color: #ff4d4f;
  font-size: 24px;
  margin-top: 4px;
}

.delete-info {
  flex: 1;
}

.delete-title {
  margin: 0 0 8px 0;
  font-size: 18px;
  font-weight: 600;
  color: #1a1a1a;
}

.delete-description {
  margin: 0 0 16px 0;
  color: #666;
  line-height: 1.5;
}

.role-preview {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px;
  background: #fafafa;
  border-radius: 8px;
  margin-bottom: 16px;
}

.role-avatar {
  flex-shrink: 0;
}

.role-details {
  flex: 1;
}

.role-name {
  font-size: 16px;
  font-weight: 600;
  color: #1a1a1a;
  margin-bottom: 8px;
}

.role-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}

.role-tag {
  font-size: 12px !important;
  border-radius: 4px !important;
}

.voice-tag {
  background: linear-gradient(135deg, #a855f7, #c084fc) !important;
  border: none !important;
  color: white !important;
}

.knowledge-tag {
  background: linear-gradient(135deg, #3b82f6, #60a5fa) !important;
  border: none !important;
  color: white !important;
}

.voice-icon,
.knowledge-icon {
  margin-right: 4px;
  font-size: 12px;
}

.confirmation-input {
  margin-bottom: 16px;
}

.input-label {
  margin: 0 0 8px 0;
  color: #333;
  font-size: 14px;
}

.error-message {
  margin-top: 4px;
  color: #ff4d4f;
  font-size: 12px;
}

.warning-notice {
  margin-top: 16px;
}

:deep(.ant-alert-warning) {
  border: 1px solid #faad14;
  background-color: #fffbe6;
}
</style>